package com.sikadwafastdaisfondturbo.pretloan.ui.user.page

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sikadwafastdaisfondturbo.lib_baseadapter.SikadwaOnItemChildClickListener
import com.sikadwafastdaisfondturbo.lib_baseadapter.SikadwaRecyclerViewAdapter
import com.sikadwafastdaisfondturbo.lib_baseadapter.SikadwaViewHolderHelper
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.databinding.PageAccountEnBinding
import com.sikadwafastdaisfondturbo.pretloan.pro.ProFragment
import com.sikadwafastdaisfondturbo.pretloan.utils.DensityUtils
import com.sikadwafastdaisfondturbo.pretloan.utils.gilde.ImageUtil
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls.addCommonParams
import com.sikadwafastdaisfondturbo.pretloan.utils.http.IHttpCallback
import com.sikadwafastdaisfondturbo.pretloan.utils.rv.LinearDecoration
import org.json.JSONObject

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/4/24 20:03
 * @Annotation：我的账户
 */
class AccountEnPage: ProFragment() {

    lateinit var binding: PageAccountEnBinding
    private var mItemAdapter: ItemAdapter? = null
    override fun getRootView(inflater: LayoutInflater): View {
        binding = PageAccountEnBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        initRefreshLayout()
        initRecyclerView()
    }

    override fun initListener() {
    }

    override fun initData() {
        showLoading()
        loadAccountAction()
    }

    /* 初始化刷新 */
    private fun initRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            loadAccountAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        binding.recyclerView.visibility = View.GONE
        binding.tipsLayout.visibility = View.GONE

        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        val topDecoration = DensityUtils.dp2px(16f)
        binding.recyclerView.addItemDecoration(LinearDecoration(topDecoration,topDecoration,topDecoration,topDecoration))
        mItemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = mItemAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mItemAdapter!!.setOnItemChildClickListener(SikadwaOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户信息
     */
    private fun setAccountList(list: ArrayList<JSONObject>){
        mItemAdapter!!.data = list

        binding.recyclerView.visibility = View.VISIBLE
        binding.tipsLayout.visibility = View.VISIBLE
    }

    class ItemAdapter(recyclerView: RecyclerView?) : SikadwaRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_account_en) {
        override fun setItemChildListener(helper: SikadwaViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: SikadwaViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.tvName,model.optString("name"))
            helper.setText(R.id.tvNumber,model.optString("account"))
            val imgLogo = helper.getImageView(R.id.imgLogo)
            ImageUtil.instance!!.loadImageUrl(mContext,imgLogo,model.optString("logo"),R.mipmap.img_holder)
        }
    }

    /**
     * 加载账户列表
     */
    private fun loadAccountAction(){
        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
        val params = JSONObject().addCommonParams().apply {
            put("path", AppUrls.PATH.ACCOUNT_LIST)
        }
        postAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                if (array != null && array.length() > 0) {
                    val accountList = arrayListOf<JSONObject>()
                    for (i in 0..<array.length()){
                        accountList.add(array.optJSONObject(i))
                    }
                    setAccountList(accountList)
                } else {
                    binding.recyclerView.visibility = View.GONE
                    binding.tipsLayout.visibility = View.GONE
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                binding.refreshLayout.finishRefresh()
            }
        })
    }
}